let foi = float_of_int

(*vitesse de rotation de translation*)
let vRot = 2.
let vTrans = 0.05

(*valeurs de rotation*)
let rotY = ref 0.
let rotX = ref 0.
let rotZ = ref 0.

(*valeurs de translation*)
let trX = ref 0.
let trY = ref 0.
let trZ = ref 0.

let zoom = ref 1.
let vZoom = 0.01

let width = ref 200
let height = ref 200

let polyMode = ref `line (*fill line point*)

let drawEnable = ref false 

let resetTrans () =
    trX := 0.;
    trY := 0.;
    trZ := 0.

let resetRot () =
    rotX := 0.;
    rotZ := 0.;
    rotY := 0.

let reset () =
    resetTrans ();
    resetRot ()

let parseTab = ref (Array.make 1 (0.,0.,0.))
let colorTab = ref (Array.make 1 (0.,0.,0.))

let area window = ref (GlGtk.area
    [`DOUBLEBUFFER; `RGBA; `DEPTH_SIZE 16; `BUFFER_SIZE 16]
    ~width: !width
    ~height: !height
    ~packing: window#add ())

let drawMap area =
    let drawPoints (v1,v2,v3) =
            GlDraw.color !colorTab.(v1 - 1);
            GlDraw.vertex3 !parseTab.(v1 - 1);
            GlDraw.color !colorTab.(v2 - 1);
            GlDraw.vertex3 !parseTab.(v2 - 1);
            GlDraw.color !colorTab.(v3 - 1);
            GlDraw.vertex3 !parseTab.(v3 - 1)
    in
    GlClear.clear [`color; `depth];
    Gl.enable `depth_test;
    GlDraw.polygon_mode `both !polyMode ;
    GlMat.mode `modelview;
    GlMat.load_identity ();
    GlMat.scale ~x:!zoom ~y:!zoom ~z:!zoom ();
    GlMat.rotate ~angle:!rotY ~x:0. ~y:1. ~z:0. ();
    GlMat.rotate ~angle:!rotX ~x:1. ~y:0. ~z:0. ();
    GlMat.rotate ~angle:!rotZ ~x:0. ~y:0. ~z:1. ();
    GlMat.translate ~x:!trX ~y:!trY ~z:!trZ ();
    GlDraw.begins `triangles;
    List.iter drawPoints !Parse.faces;
    GlDraw.ends ();
    Gl.flush ();
    !area#swap_buffers ()

let drawGL area () =
    if !drawEnable then
            ignore(drawMap area)

(*gere les entrees clavier*)
let inputManager ev =
    let key = GdkEvent.Key.keyval ev in
    if !drawEnable then
        match key with
            k when k = GdkKeysyms._Right-> rotY := !rotY -. vRot
            |k when k = GdkKeysyms._Left -> rotY:= !rotY +. vRot
            |k when k = GdkKeysyms._Up -> rotX := !rotX +. vRot
            |k when k = GdkKeysyms._Down -> rotX := !rotX -. vRot
            |k when k = GdkKeysyms._u -> rotZ := !rotZ +. vRot
            |k when k = GdkKeysyms._o -> rotZ := !rotZ -. vRot
            |k when k = GdkKeysyms._w -> trY := !trY +. vTrans
            |k when k = GdkKeysyms._s -> trY := !trY -. vTrans
            |k when k = GdkKeysyms._d -> trX := !trX +. vTrans
            |k when k = GdkKeysyms._a -> trX := !trX -. vTrans
            |k when k = GdkKeysyms._i -> zoom := !zoom +. vZoom
            |k when k = GdkKeysyms._k -> zoom := !zoom -. vZoom
            |k when k = GdkKeysyms._r -> resetTrans (); resetRot ()
            |k when k = GdkKeysyms._f -> polyMode := `fill
            |k when k = GdkKeysyms._l -> polyMode := `line
            |k when k = GdkKeysyms._p -> polyMode := `point
            |_ -> ()
    else
        ()

let drawNewFile path hrgb =
    drawEnable := true;
    reset ();
    parseTab := Parse.main path;
    colorTab := Parse.colorTab !parseTab hrgb 

let main window realWindow =
    let area = area window in
    ignore(realWindow#event#connect#key_press ~callback:
        begin fun ev ->
            if !drawEnable then
                begin
                    ignore(inputManager ev);
                    drawGL area ()
                end
            else
                ignore(inputManager ev);
            true
        end);
    ignore(!area#connect#display ~callback:(drawGL area))
